package cc.zy.base.businesses.service.impl;


import cc.zy.base.businesses.entity.ErrorLog;
import cc.zy.base.businesses.entity.NetworkSchool;
import cc.zy.base.businesses.mapper.EntranceScoreMapper;
import cc.zy.base.businesses.mapper.NetworkSchoolMapper;
import cc.zy.base.businesses.service.INetworkSchoolService;
import cc.zy.base.businesses.utils.ReadAccountExcel;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 *  Service实现
 *
 * @author jiangjinlin
 * @date 2021-01-22
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Slf4j
public class NetworkSchoolServiceImpl implements INetworkSchoolService {

    private final NetworkSchoolMapper networkSchoolMapper;

    private final EntranceScoreMapper entranceScoreMapper;

    @Override
    public boolean saveBatch(Collection<NetworkSchool> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<NetworkSchool> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<NetworkSchool> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(NetworkSchool entity) {
        return false;
    }

    @Override
    public NetworkSchool getOne(Wrapper<NetworkSchool> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<NetworkSchool> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<NetworkSchool> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<NetworkSchool> getBaseMapper() {
        return null;
    }

    @Override
    public NetworkSchool findByIdAndOpenId(Integer id) {

        NetworkSchool networkSchool = networkSchoolMapper.getByIdAndOpenId(id);
        return networkSchool;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean allocationStu(NetworkSchool networkSchool) {

        int i = networkSchoolMapper.updateNetworkSchool(networkSchool);
        if (i > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public IPage<NetworkSchool> listNetworkSchool(QueryRequest request, NetworkSchool networkSchool) {
        Page<NetworkSchool> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(networkSchoolMapper.countNetworkSchool(networkSchool));
        SortUtil.handlePageSort(request, page, "sendTime", FebsConstant.ORDER_DESC, true);
        return networkSchoolMapper.listNetworkSchool(page, networkSchool);
    }

    @Override
    public List<Map<String, Object>> findPrice() {

        List<Map<String, Object>> map = networkSchoolMapper.selectOriginalPrice();

        return map;
    }

    @Override
    @Transactional
    public String readExcel(MultipartFile multipartFile, HttpSession session) {

        List<NetworkSchool> list = null;
        ArrayList<String> username = new ArrayList<>();
        String code = "";
        try {
            //读取excel数据
            FebsResponse febsResponse = ReadAccountExcel.readData(multipartFile);
            String message = (String) febsResponse.get("message");
            if (message.equals("200")) {
                //成功获取excel中的数据
                List<NetworkSchool> networkSchools = (List<NetworkSchool>) febsResponse.get("data");
                log.debug(String.format("networkSchools = %s", networkSchools));

                for (NetworkSchool networkSchool : networkSchools) {
                    if ("已激活".equals(networkSchool.getStatus())){
                        networkSchool.setActivate(2);
                    }else  if ("未激活".equals(networkSchool.getStatus())){
                        networkSchool.setActivate(1);
                    }
                }

                int i = networkSchoolMapper.addUsername(networkSchools);

                log.debug(String.format("i = %s", i));

                code = "200";
            } else {
                List<ErrorLog> errorLogs = (List<ErrorLog>) febsResponse.get("data");
                String unique = errorLogs.get(0).getUnique();
                session.setAttribute("unique", unique);

                entranceScoreMapper.addErrorLogList(errorLogs);
                code = "400";
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return code;
    }

    @Override
    public List<NetworkSchool> listNetwork(NetworkSchool networkSchool) {

        List<NetworkSchool> networkSchools = networkSchoolMapper.listNetworkSchool(networkSchool);
        return networkSchools;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean resetNetworkSchool(String username) {

        int i = networkSchoolMapper.resetUsername(username);
        if (i > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addremark(String remark, String username) {

        int i = networkSchoolMapper.addRemark(remark, username);

        if (i > 0){
            return true;
        }else {
            return false;
        }
    }

}